package com.gexin.common.app;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.gexin.common.app.JpushModel.PushType;
import com.jfinal.log.Log;

/**
 * 极光推送的接口
 * 
 * @author 肖得明
 * 
 */
public class JpushToolsApi {
	private  static Log log = Log.getLog(JpushToolsApi.class);
	private static final String appKey ="92ca669850ba18eb6ca2d37d";
	private static final String masterSecret = "b61f9831d8fbc5c73f6bec54";
	private static  JPushClient client  ;
	public static JPushClient  getJPushClient (){
		if(client==null){
			 client = new JPushClient(masterSecret,appKey, 3);
		}
		return client;
	}
	
	/**
	 * 推送消息到App 内部 不会在任务栏通知
	 * 肖得明  第一版  20160821
	 * @param audience     别名
	 * @param type     事件类型
	 * @param data     数据
	 */
	public static void Jpuls_App_InMessage(Audience audience,String content,String type,String data){
		  //消息构造
		 Message msg = Message.newBuilder()
             		.setMsgContent(content).addExtra("type", type).addExtra("data", 1).build();
             		PushPayload  payload=  PushPayload.newBuilder().setPlatform(Platform.android_ios())
                   .setAudience(audience)
                   .setMessage(msg).build();
             		JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
             try {
                 jpushClient.sendPush(payload);
				} catch (Exception e) {
					log.error("推送消息错误！");
				}
	}
	
	
	public static  void startPush(JpushModel bean){
		if(bean.getPush_type()== PushType.inner){
			//内部消息 	 //消息构造
			Message msg = Message.newBuilder()
     		.setMsgContent(bean.getCentent())
     		.setTitle(bean.getTitle())
     		.addExtra("type", bean.getType())//事件类型
     		.addExtra("data", bean.getDataToJson())//数据
     		.addExtra("silent", bean.isSilent())//是否提示
     		.addExtra("push_type","inner")//推送类型
     		.build();
     	    PushPayload  payload=  PushPayload.newBuilder().setPlatform(Platform.android_ios())
           .setAudience(bean.getAudience())
           .setMessage(msg).build();
     	   JPushClient jpushClient = getJPushClient();
           try {
             jpushClient.sendPush(payload);
			} catch (Exception e) {
				e.printStackTrace();
				log.error("推送消息错误！");
			}
		}else{
			//====================================app 内部
			 PushPayload  payload =   PushPayload.newBuilder()
		                .setPlatform(Platform.android_ios())
		                .setAudience(bean.getAudience())
		                .setOptions(Options.newBuilder()
		                		.setApnsProduction(true)//设置发送IOS生产模式
		                		//.setTimeToLive(0)//设置只发送在线的
		                		.build())
		                .setNotification(Notification.newBuilder()
		                		.setAlert(bean.getCentent())
		                		.addPlatformNotification(AndroidNotification.newBuilder()
		                				.setTitle(bean.getTitle())
	                					.addExtra("type", bean.getType())//事件类型
							     		.addExtra("data", bean.getDataToJson())//数据
							     		.addExtra("silent", bean.isSilent())//是否提示
							     		.addExtra("push_type","outer")//推送类型
		                				.build())
		                		.addPlatformNotification(IosNotification.newBuilder()
		                				.incrBadge(+1)
		                				.addExtra("type", bean.getType())//事件类型
							     		.addExtra("data", bean.getDataToJson())//数据
							     		.addExtra("silent", bean.isSilent())//是否提示
							     		.addExtra("push_type","outer")//推送类型
		                				.build())
		                		.build())
		                .build();
	            try {
	            	JPushClient jpushClient = getJPushClient();
					//PushResult result = 
	            	jpushClient.sendPush(payload);
				} catch (Exception e) {
					e.printStackTrace();
					log.error("推送消息错误！");
				}
		}
	}
}
